﻿using AutoMapper;
using B.S.Filtration.Domain.工单;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.CustomerPhoneCommand;
using Google.Protobuf.WellKnownTypes;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.CustomerPhoneCommandHander
{
    public class UpdCustomerPhoneCommandHander:IRequestHandler<UpdCustomerPhoneCommand,APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public UpdCustomerPhoneCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(UpdCustomerPhoneCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<CustomerPhone>(request);
            model.Del = false;

            result.data = await db.Updateable<CustomerPhone>(model).ExecuteCommandAsync();

            if (result.data > 0)
            {
                result.code = APIResultCode.success;
                result.msg = "修改客户信息成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "修改客户信息失败";
            }

            return await Task.FromResult(result);
        }
    }
}
